package ua.kharkov.khpi.tarasenko.bach.web.command.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.khpi.tarasenko.bach.core.Constant;
import ua.kharkov.khpi.tarasenko.bach.core.user.ShoppingCart;
import ua.kharkov.khpi.tarasenko.bach.core.user.UserBean;
import ua.kharkov.khpi.tarasenko.bach.web.command.Command;

/**
 * Command to empty shopping cart.
 * 
 * @author oleksandr
 *
 */
public class ClearShoppingCart extends Command {
	private static final Logger log = Logger.getLogger(ClearShoppingCart.class);

	private static final String ATTRIBUTE_IS_CLEARED = "isCleared";

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) {

		String forward = Constant.PAGE_CLEAR_SHOPPING_CART_RESULT;
		boolean isCleared = false;

		// Get cart from user bean and assign to it a new cart.
		HttpSession session = request.getSession();
		UserBean userBean = (UserBean) session
				.getAttribute(Constant.ATTRIBUTE_USER_BEAN);
		userBean.setShoppingCart(new ShoppingCart());

		isCleared = true;
		request.setAttribute(ATTRIBUTE_IS_CLEARED, isCleared);
		if (log.isTraceEnabled()) {
			log.trace("Set request attribute " + ATTRIBUTE_IS_CLEARED + "="
					+ isCleared);
		}

		if (log.isDebugEnabled()) {
			log.debug("Forward to " + forward);
		}
		return forward;
	}
}
